//
//  JSAllEvaluaController.m
//  jshop
//
//  Created by 张艳江 on 2019/5/15.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCAllEvaluaController.h"
#import "SCGoodsCommentCell.h"

@interface SCAllEvaluaController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView    *tableView;
@property (strong, nonatomic) NSMutableArray *commentArray;

@property (assign, nonatomic) int  pageIndex;

@end

@implementation SCAllEvaluaController

- (NSMutableArray *)commentArray{
    if (!_commentArray) {
        _commentArray = [NSMutableArray array];
    }
    return _commentArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"全部评价";
    self.lineView.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    //设置taleView
    [self setUpTableView];
    //请求评论数据
    self.pageIndex = 1;
    [SVProgressHUD showWithStatus:@"加载中..."];
    [self requestCommentData];
    
    // 添加尾部刷新
    [self addHeaderFooterRefreshing];
}
#pragma mark - 加载/刷新
- (void)addHeaderFooterRefreshing{
    
    self.tableView.mj_header = [SCRefreshMyHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self requestCommentData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex++;
        [self requestCommentData];
    }];
}
- (void)setUpTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0.5, kScreenW, kScreenH-kTopHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = BGColor;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[SCGoodsCommentCell class] forCellReuseIdentifier:@"SCGoodsCommentCellID"];
    [self.view addSubview:self.tableView];
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:kScreenW tableView:self.tableView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SCGoodsCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCGoodsCommentCellID"];
    cell.model = self.commentArray[indexPath.row];
    return cell;
}
#pragma mark - 请求评论数据
-  (void)requestCommentData{

    NSString *urlStr = [MyTool splicingWithUrl:self.url];
    NSDictionary *dict = @{@"pagenow":@(self.pageIndex),
                           @"size":@"10",
                           self.type:self.goodsId};
    //NSLog(@"提交的参数%@",dict);
    [[NetworkRequestTool sharedNetworkTool] POST:urlStr parameters:dict finished:^(id result, NSString *error) {
        NSLog(@"===%@",result);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        if (self.pageIndex == 1) {
            [self.commentArray removeAllObjects];
        }
        NSArray *tempArray;
        if ([self.type isEqualToString:@"gift_id"] || [self.type isEqualToString:@"goods_id"]) {
            tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"comment_list"]];
        }else{
            tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
        }
        [self.commentArray addObjectsFromArray:tempArray];
        [self.tableView reloadData];
        
        if (tempArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}



@end
